# -*- coding: utf-8 -*-

import random
import urllib2, requests
import json
import redis
from config.setting import SEARCHINFO_REDIS, RESTINFO_REDIS, SEARCHINFO_KEY, RESTPICKEY, RESTINFOKEY, REST_LIST_KEY, REST_LIST_YT_KEY
import codecs

import sys
reload(sys)
sys.setdefaultencoding('gbk')


regionPath = '/home/maintainer/dianping/dianping/spiders/data/Region.txt'
censusPath = '/home/maintainer/dianping/dianping/spiders/data/Cusine.txt'
cityinfoPath = '/home/maintainer/dianping/dianping/spiders/data/City.txt'
respicPath = '/home/maintainer/dianping/dianping/spiders/data/respic.txt'
RESTINFO_REDIS
#agent = 'http://user:pass@127.0.0.1:3128/'

class DianPInit(object):
    def __init__(self):
        self.cityInfoList = {}
        self.pool = redis.ConnectionPool(host=SEARCHINFO_REDIS['host'], port=SEARCHINFO_REDIS['port'], db=SEARCHINFO_REDIS['db'])
        self.rds = redis.Redis(connection_pool=self.pool)
        
        self.pool = redis.ConnectionPool(host=RESTINFO_REDIS['host'], port=RESTINFO_REDIS['port'], db=RESTINFO_REDIS['db'])
        self.rds2 = redis.Redis(connection_pool=self.pool)
        
        
        #self.cityInfoList = self.rds.get('dianp:cityInfoList')
    
    
    def init_cityinfo(self):
        fp = open(cityinfoPath, 'r')
        flag = 0
        for line in fp:
            if flag == 0 :
                flag = flag + 1
                continue
            cityinfo = line.split('|')
            self.cityInfoList[cityinfo[0]] =  {'hotregionList':[], 'cusineList':[]}
            #print  cityinfo[0]
        
        flag = 0
        fp = open(regionPath, 'r')
        for line in fp:
            if flag == 0 :
                flag = flag + 1
                continue
            regioninfo = line.split('|')
            city = regioninfo[1]
            region = regioninfo[0]
            self.cityInfoList[city]['hotregionList'].append(region)
        
        flag = 0
        fp = open(censusPath, 'r')
        for line in fp:
            if flag == 0:
                flag = flag + 1
                continue
            cenusinfo = line.split('|')
            city = cenusinfo[1]
            census = cenusinfo[0]
            self.cityInfoList[city]['cusineList'].append(census)
        #print self.cityInfoList
        
        
    def send_redis(self):
        #print SEARCHINFO_REDIS
        print self.rds.set(SEARCHINFO_KEY, json.dumps(self.cityInfoList))
        #print self.rds.delete(SEARCHINFO_REDIS)
    
    def init_picInfo(self):
        flag = 0
        fp = open(respicPath, 'r')
        for line in fp:
            if flag == 0:
                flag = flag + 1
                continue
            picinfo = line.split('|')
            restid = picinfo[0]
            restid = restid.replace('\x00','')
            picids = picinfo[2]
            picids = picids.replace('\x00','')
            picarr = picids.split(',')
            for picid in picarr[:-1]:
                #print RESTPICKEY(restid)
                self.rds2.sadd(RESTPICKEY(restid), picid)
    
    def del_picinfo(self):
        flag = 0
        fp = open(respicPath, 'r')
        for line in fp:
            if flag == 0:
                flag = flag + 1
                continue
            picinfo = line.split('|')
            restid = picinfo[0]
            self.rds2.delete(RESTPICKEY(restid))
        
            
    def init_restlist(self):
        #self.rds.delete(REST_LIST_KEY)
        flag = 0
        fp = open(respicPath, 'rb')
        for line in fp:
            if flag == 0:
                flag = flag + 1
                continue
            picinfo = line.split('|')
            restid = picinfo[0]
            self.rds.sadd(REST_LIST_KEY, restid.replace('\x00','')) 
            #break

    def init_restlist_test(self):
        #self.rds.delete(REST_LIST_KEY)
        flag = 0
        fp = open(respicPath, 'rb')
        for line in fp:
            if flag == 0:
                flag = flag + 1
                continue
            picinfo = line.split('|')
            if len(picinfo) < 2:
                continue
            
            restid = picinfo[0]
            cityid = picinfo[1]
            cityid = cityid.replace('\x00','')
            if cityid != '139':
                continue
            self.rds.sadd(REST_LIST_YT_KEY, restid.replace('\x00','')) 
            #break
            
            
        

init = DianPInit()
init.init_restlist_test()
#init.init_picInfo()
#init.init_cityinfo()
#init.send_redis()
